package neu.soft.controller;

import neu.soft.entity.Good;
import neu.soft.exception.AppException;
import neu.soft.exception.AppExceptionCodeMsg;
import neu.soft.query.GoodPageQuery;
import neu.soft.service.GoodsService;
import neu.soft.vo.PageBean;
import neu.soft.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/goods")
@CrossOrigin
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @PostMapping
    public ResultBean addGood(@RequestBody Good good) {

        if (good == null) {
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }
        int res = goodsService.addGood(good);
        return ResultBean.success("商品登记成功");
    }

    @PutMapping
    public ResultBean updateGood(@RequestBody Good good) {

        int res = goodsService.updateGood(good);
        return ResultBean.success("商品更新成功");
    }

    @PostMapping("/delete")
    public ResultBean deleteGood(@RequestBody Map<String,Object> map) {

        String goodId = (String) map.get("goodId");
        int res = goodsService.deleteGood(goodId);
        return ResultBean.success("商品删除成功");
    }

    @PostMapping("/page")
    public ResultBean<PageBean<Good>> findByPage(@RequestBody GoodPageQuery goodQuery) {

        PageBean<Good> goodList = goodsService.findByPage(goodQuery);
        return ResultBean.success(goodList);
    }

    @GetMapping
    public ResultBean<Map<String, List<String>>> findAllType() {
        return ResultBean.success(goodsService.findAllInfo());
    }

    @PostMapping("/batchFind")
    public List<Good> findGoods(@RequestBody List<String> code) {
        return goodsService.batchFind(code);
    }

}
